package com.xjscrm.console.common.web;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebFilter(filterName = "CorsFilter")
@Configuration
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse servletResponse = (HttpServletResponse) response;
        servletResponse.setHeader("Access-Control-Allow-Origin","*");
        servletResponse.setHeader("Access-Control-Allow-Credentials","true");
        servletResponse.setHeader("Access-Control-Allow-Methods","POST, GET, PATCH, DELETE, PUT");
        servletResponse.setHeader("Access-Control-Max-Age","3600");
        servletResponse.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept,Auth-Token,Refresh-Token");
        servletResponse.setHeader("Access-Control-Expose-Headers","Auth-Token,Refresh-Token");
        chain.doFilter(request,response);
    }
}
